﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsAcess.OLEDB;

namespace WindowsFormsAcess
{
    public partial class DBPerformance : Form
    {
        #region 初始化和构造
        private OLEDB.DBFairy dbFairy = null;
        private OLEDB.DBFairy dbFairyOracle = null;
        private OLEDB.DBFairy dbFairyAcess = null;
        public DBPerformance()
        {
            InitializeComponent();

            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WindowsFormsAcess.Properties.Settings.OLEDBConnectionString"].ConnectionString;
            string oracleCNString = System.Configuration.ConfigurationManager.ConnectionStrings["WindowsFormsAcess.Properties.Settings.OracleConnectionString"].ConnectionString;
            string accessCNString = System.Configuration.ConfigurationManager.ConnectionStrings["WindowsFormsAcess.Properties.Settings.AccessConnectionString"].ConnectionString;

            dbFairy = new OLEDB.DBFairy(connectionString, DBFairy.DBType_MSSQL, false);
            dbFairyOracle = new OLEDB.DBFairy(oracleCNString, DBFairy.DBType_Oracle, false);
            dbFairyAcess = new OLEDB.DBFairy(accessCNString, DBFairy.DBType_ACCESS, false);
        }
        public string AppendString = "INSERT INTO Students (ID,STUNO,NAME,AGE,SEX,DESCRIPTION ) VALUES (?,?,?,?,?,?)";
        public string UpdateString = "Update Students SET DESCRIPTION = ? Where NAME = ?";

        private void comboBoxCommondType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
        private System.Diagnostics.Stopwatch stopWatchOracle = new System.Diagnostics.Stopwatch();
        private System.Diagnostics.Stopwatch stopWatchAcess = new System.Diagnostics.Stopwatch();

        private void btnBegin_Click(object sender, EventArgs e)
        {
            completed = 0;
            lblMSSQL.ForeColor = lblAceesss.ForeColor = lblOracle.ForeColor = Color.Black;

            this.numericUpDownOracle.Value = numericUpDownAcess.Value = numericUpDownMSSQL.Value = 0;

            bgAcessWorker.RunWorkerAsync();
            bgMSWorker.RunWorkerAsync();
            bgOracleWorker.RunWorkerAsync();


        }
        int completed = 0;
        Color[] colorsGrade = { Color.DarkGreen, Color.DodgerBlue, Color.DarkGoldenrod };
        private void DBPerformance_Load(object sender, EventArgs e)
        {
            lblFirst.BackColor = colorsGrade[0];
            lblSecond.BackColor = colorsGrade[1];
            lblThird.BackColor = colorsGrade[2];
            numericUpDownMSSQL.Maximum = numericUpDownAcess.Maximum = numericUpDownOracle.Maximum = txtCount.Maximum = int.MaxValue;

        }
        #endregion

        #region 执行任务
        /// <summary>
        /// 执行插入操作
        /// </summary>
        /// <param name="i"></param>
        /// <param name="dbFairy"></param>
        /// <returns></returns>
        private int DoAppendDBWork(int i, DBFairy dbFairy)
        {
            System.Data.OleDb.OleDbParameter id = null;
            if (dbFairy.DBType == DBFairy.DBType_Oracle)
            {
                id = new System.Data.OleDb.OleDbParameter("ID", DBFairy.DotNetToOracle(Guid.NewGuid().ToString()));
            }
            else
            {
                id = new System.Data.OleDb.OleDbParameter("ID", Guid.NewGuid().ToString());
            }
            System.Data.OleDb.OleDbParameter[] paras = new System.Data.OleDb.OleDbParameter[]{
              
                    id,
                    new System.Data.OleDb.OleDbParameter("STUNO", 2009430+i),
                    new System.Data.OleDb.OleDbParameter("NAME", "姓名"+i),
                     new System.Data.OleDb.OleDbParameter("AGE",20+ i%2),
                    new System.Data.OleDb.OleDbParameter("SEX", i%2),
                      new System.Data.OleDb.OleDbParameter("DESCRIPTION", DateTime.Now.ToShortDateString())

                    };

            return dbFairy.ExecuteNonQuery(AppendString, paras);
        }
        #endregion

        #region MS SQL
        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled && e.Error == null)
            {
                stopWatch.Stop();

                numericUpDownMSSQL.Value = stopWatch.ElapsedMilliseconds;

                lblMSSQL.ForeColor = colorsGrade[completed];
                completed++;

            }

        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                stopWatch.Reset();
                stopWatch.Start();
                int count = Convert.ToInt32(txtCount.Value);
                for (int i = 0; i < count; i++)
                {

                    int result = DoAppendDBWork(i, dbFairy);
                    if (result < 1)
                    {
                        string desc = string.Empty;
                        bgMSWorker.ReportProgress(i, desc);
                    }
                    else
                    {
                        bgMSWorker.ReportProgress(i);
                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }



        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            try
            {
                if (e.UserState != null)
                {
                    richTextBoxMSSQL.AppendText("Error: At" + e.ProgressPercentage.ToString() + Environment.NewLine);
                }
                else
                {
                    richTextBoxMSSQL.AppendText(e.ProgressPercentage.ToString() + Environment.NewLine);
                }
                richTextBoxMSSQL.SelectionStart = richTextBoxMSSQL.Text.Length;
                this.richTextBoxMSSQL.ScrollToCaret();
                numericUpDownMSSQL.Value = stopWatch.ElapsedMilliseconds;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion


        #region Oracle
        private void bgOracleWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            stopWatchOracle.Reset();
            stopWatchOracle.Start();

            int count = Convert.ToInt32(txtCount.Value);
            for (int i = 0; i < count; i++)
            {

                int result = DoAppendDBWork(i, dbFairyOracle);
                if (result < 1)
                {
                    string desc = string.Empty;
                    bgOracleWorker.ReportProgress(i, desc);
                }
                else
                {
                    bgOracleWorker.ReportProgress(i);
                }
            }
        }

        private void bgOracleWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            try
            {
                if (e.UserState != null)
                {
                    richTextBoxOracle.AppendText("Error: At" + e.ProgressPercentage.ToString() + Environment.NewLine);
                    richTextBoxOracle.AppendText(e.UserState.ToString());
                }
                else
                {
                    richTextBoxOracle.AppendText(e.ProgressPercentage.ToString() + Environment.NewLine);
                }
                richTextBoxOracle.SelectionStart = richTextBoxOracle.Text.Length;

                this.richTextBoxOracle.ScrollToCaret();
                numericUpDownOracle.Value = stopWatchOracle.ElapsedMilliseconds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void bgOracleWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled && e.Error == null)
            {
                stopWatch.Stop();

                numericUpDownOracle.Value = stopWatchOracle.ElapsedMilliseconds;

                lblOracle.ForeColor = colorsGrade[completed];

                completed++;


            }
        }
        #endregion

        #region Acess
        private void bgAcessWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            stopWatchAcess.Reset();
            stopWatchAcess.Start();
            int count = Convert.ToInt32(txtCount.Value);
            for (int i = 0; i < count; i++)
            {

                int result = DoAppendDBWork(i, dbFairyAcess);
                if (result < 1)
                {
                    string desc = string.Empty;
                    bgAcessWorker.ReportProgress(i, desc);
                }
                else
                {
                    bgAcessWorker.ReportProgress(i);
                }
            }
        }

        private void bgAcessWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            try
            {
                if (e.UserState != null)
                {
                    richTextBoxAcess.AppendText("Error: At" + e.ProgressPercentage.ToString() + Environment.NewLine);
                    richTextBoxAcess.AppendText(e.UserState.ToString());
                }
                else
                {
                    richTextBoxAcess.AppendText(e.ProgressPercentage.ToString() + Environment.NewLine);
                }
                richTextBoxAcess.SelectionStart = richTextBoxAcess.Text.Length;
                this.richTextBoxAcess.ScrollToCaret();

                numericUpDownAcess.Value = stopWatchAcess.ElapsedMilliseconds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void bgAcessWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled && e.Error == null)
            {
                stopWatchAcess.Stop();


                numericUpDownAcess.Value = stopWatchAcess.ElapsedMilliseconds;

                lblAceesss.ForeColor = colorsGrade[completed];
                completed++;

            }

        }
        #endregion
    }
}
